package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;

public class ChatPage extends Utils {
    public static String clienrUrl = "http://127.0.0.1:8888/client.html";
    public static String loginUrl = "http://127.0.0.1:8888/login.html";

    public ChatPage() {
        super("http://127.0.0.1:8888/login.html");
    }

//    先登录
    public void loginSuss(){
//        driver.get(loginUrl);
        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        driver.findElement(By.cssSelector("#inputCaptcha")).sendKeys("26588");
//        点击登录按钮
        driver.findElement(By.cssSelector("#submit")).click();
//        处理弹窗
//        隐式等待不起作用，警示框需要加显示等待
        new WebDriverWait(driver, Duration.ofSeconds(3)).until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        alert.accept();
//        检查点击登录后是否登录成功，可以通过聊天页面特有的元素进行判断
        driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.tab > div.tab-session"));
        driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.tab > div.tab-friend"));
//        成功登录
    }

//  11.聊天页面 -- 在登录前对网址：http://127.0.0.1:8888/client.html进行访问
    public void noLoginChatPage() throws IOException, InterruptedException {
//        Thread.sleep(1000);
        //处理警告弹窗
        driver.get(loginUrl);
        Alert alert = driver.switchTo().alert();
        alert.accept();
        //调整到登录页面
        String expect = driver.getTitle();
        assert expect.equalsIgnoreCase("login");
//        getScreenShot(getClass().getName());
        driver.quit();
    }

//    12+13.聊天页面 -- 检查聊天页面是否正常加载
    public void chatroomPageRight() throws IOException {
        loginSuss();
//        getScreenShot("chatroomPageRight");
        driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.user-list > div > span"));
        String title = driver.getTitle();
        assert title.equals("网页聊天程序");
//        driver.quit();
    }

//    14.点击会话标签  -->  检查截图
    public void checkSessionImage() throws IOException {
        driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.tab > div.tab-session")).click();
//        getScreenShot("checkSessionImage");
    }

//    15.点击好友标签
    public void checkFriendImage() throws IOException {
        driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.tab > div.tab-friend")).click();
//        getScreenShot("checkFriendImage");
        driver.findElement(By.cssSelector("#friend-list > li:nth-child(1) > h4"));
        driver.findElement(By.cssSelector("#friend-list > li:nth-child(2) > h4"));
    }

//    16.点击具体的好友，点击好友”list“
    public void clickFriend() throws IOException, InterruptedException {
        driver.findElement(By.cssSelector("#friend-list > li:nth-child(1)")).click();
//        getScreenShot("checkFriendSession");
        Thread.sleep(1000);
//        因为，我没分清楚是会话列表的list还是好友列表的list
//        String text = driver.findElement(By.cssSelector("#friend-list > li:nth-child(1) > h4")).getText();
        String text = driver.findElement(By.xpath("//*[@id=\"session-list\"]/li[1]/div/h3")).getText();
//        System.out.println(text);
        assert text.equals("list");
//        driver.quit();
    }

//    17.点击具体的会话，点击“测试会话3”
    public void clickSession() throws IOException {
        driver.findElement(By.cssSelector("#session-list > li:nth-child(2)")).click();
//        getScreenShot("clickSession");
        String text = driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.message-show > div:nth-child(1) > div > p")).getText();
        assert text.equals("啊？");
//        driver.quit();
    }

//    18.点击“测试会话3”，在输入框输入“啊啊啊啊啊”
    public void sendMessage1() throws IOException, InterruptedException {
        driver.findElement(By.cssSelector("#session-list > li.selected")).click();
        driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/textarea")).sendKeys("aaaaaaaa");
        driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.ctrl > button")).click();
        driver.navigate().refresh();
        String text = driver.findElement(By.cssSelector("#session-list > li:nth-child(1) > div > p")).getText();
        System.out.println(text);
//        driver.quit();
    }

//    19.点击“王五”，在输入框输入“真的假的”
    public void sendMessage2() throws InterruptedException {
        driver.findElement(By.cssSelector("#session-list > li:nth-child(3)")).click();
        driver.findElement(By.cssSelector("body > div.client-container > div > div.right > textarea")).sendKeys("抱枕");
        driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.ctrl > button")).click();
        driver.navigate().refresh();
//        Thread.sleep(1000);
        String text = driver.findElement(By.cssSelector("#session-list > li:nth-child(2) > div > p")).getText();
        System.out.println(text);
        driver.quit();
    }
}
